<!DOCTYPE html>
<body>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script>
'use strict';

function createDateInput(min, max, value) {
    let input = document.createElement('input');
    input.type = 'date';
    if (min)
        input.min = min;
    if (max)
        input.max = max;
    if (value)
        input.value = value;
    return input;
}

// FIXME: Rename this function and the test file.
function isReadOnlyField(input, pseudo) {
    if(!window.internals)
        assert_unreached('This requires window.internals.');

    let node = internals.shadowRoot(input).querySelector('*[pseudo="' + pseudo + '"]');
    if (!node)
        assert_unreached('Requested node is missing.');
    return node.hasAttribute('disabled')  && (node.getAttribute('aria-disabled') == "true");
}

function assertReadOnly(field, input) {
    let pseudo = `-webkit-datetime-edit-${field}-field`;
    assert_true(isReadOnlyField(input, pseudo), `The ${field} spinbutton should be read-only`)
}

function assertNotReadOnly(field, input) {
    let pseudo = `-webkit-datetime-edit-${field}-field`;
    assert_false(isReadOnlyField(input, pseudo), `The ${field} spinbutton should not be read-only`)
}

test(()=>{

    assertNotReadOnly('year', createDateInput("", "", ""));
    assertNotReadOnly('year', createDateInput("2012-12-17", "", ""));
    assertNotReadOnly('year', createDateInput("", "2012-12-17", ""));
    assertNotReadOnly('year', createDateInput("", "", "2012-12-17"));
    assertReadOnly('year', createDateInput("2012-01-01", "2012-12-31", ""))
    assertNotReadOnly('year', createDateInput("2012-01-01", "2013-12-31", ""));
    assertReadOnly('year', createDateInput("2012-01-01", "2012-12-31", "2012-05-30"))
    assertNotReadOnly('year', createDateInput("2012-01-01", "2012-12-31", "2013-01-01"));

}, 'The year spinbutton in input[type=date] should be read-only in some cases.')

test(()=>{

    assertNotReadOnly('month', createDateInput("", "", ""));
    assertNotReadOnly('month', createDateInput("2012-12-17", "", ""));
    assertNotReadOnly('month', createDateInput("", "2012-12-17", ""));
    assertNotReadOnly('month', createDateInput("", "", "2012-12-17"));
    assertReadOnly('month', createDateInput("2012-12-01", "2012-12-31", ""))
    assertNotReadOnly('month', createDateInput("2012-11-01", "2013-12-31", ""));
    assertNotReadOnly('month', createDateInput("2012-12-01", "2013-12-31", ""));
    assertReadOnly('month', createDateInput("2012-12-01", "2012-12-31", "2012-12-15"))
    assertNotReadOnly('month', createDateInput("2012-12-01", "2012-12-31", "2012-11-30"));

}, 'The month spinbutton in input[type=date] should be read-only in some cases.')

test(()=>{

    assertNotReadOnly('day', createDateInput("", "", ""));
    assertNotReadOnly('day', createDateInput("2012-12-17", "", ""));
    assertNotReadOnly('day', createDateInput("", "2012-12-17", ""));
    assertNotReadOnly('day', createDateInput("", "", "2012-12-17"));
    assertNotReadOnly('day', createDateInput("2012-12-17", "2012-12-17", ""));
    assertNotReadOnly('day', createDateInput("2012-12-17", "2013-12-18", ""));
    assertNotReadOnly('day', createDateInput("2012-11-17", "2012-12-17", ""));
    assertNotReadOnly('day', createDateInput("2012-12-17", "2013-12-17", ""));
    assertNotReadOnly('day', createDateInput("2012-12-17", "2012-12-17", "2012-12-17"));
    assertNotReadOnly('day', createDateInput("2012-12-17", "2012-12-17", "2012-12-18"));

}, 'The day spinbutton in input[type=date] should be always editable.')

</script>
</body>

